﻿//Remove RobustDebug for production applications
load("MyTrace.asc");
load("RobustDebug.asc");
load("LoadConfigFun.asc");
load("ConnectMServerAndReport.asc");
load("ChatGroupManager.asc");
load("ChatGroup.asc");
//
var configs;
var ServerInstance;
function initVars(){
	configs=LoadConfigFile("config.txt");
	ServerInstance=application.name;
	ReportConfig.MNGServerURL=configs.managerServerUrl;
	ReportConfig.reportLoopTime=Number(configs.reportPreSecond);
	ReportConfig.serverURL=ServerInstance;
}
application.onAppStart = function()
{
	initVars();
	trace("["+application.name+"]started!!");
	ReportStartRun();
}
application.onAppStop = function(info)
{
	ReportStopRun();
}
application.onConnect = function(client,clientType,args)
{
	client.app=this;
	client.clientType=clientType;
	trace(client.ip+"["+clientType+"] connected.")
	if(args.length<3){
		trace("[CT] args error!");
		application.rejectConnection(client,{code:1001,error:"login in args error!"});
		return;
	}
	var sign=args[0];
	client.sign=args[0];
	if(sign=="CT"){
		AddToChatGroup(client,args[1],args[2]);
	}else{
		trace("Error connect user!");
	}
}
application.onDisconnect = function(client)
{
	if(client.sign=="CT"){
		DelFromChatGroup(client);
	}
}




function GetTimeStr(str1){
	if(str1==undefined){
		str1=" ";
	}
	var datetime=(new Date());
	var dtStr="";
	dtStr+=(datetime.getYear()+1900).toString()+"-";
	dtStr+=(datetime.getMonth()+1).toString()+"-";
	dtStr+=(datetime.getDate()).toString()+str1;
	dtStr+=(datetime.getHours()).toString()+":";
	dtStr+=(datetime.getMinutes()).toString()+":";
	dtStr+=(datetime.getSeconds()).toString();
	return dtStr;
}
function CallClient(c,FunName,msg){
	c.call(FunName,null,msg);
}

function conectStatus(info){
	for(var i in info){
		trace(i+": "+info[i]);
	}
}